home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uniterm.zoo / unidemo.c < prev    next >
C/C++ Source or Header  |  1990-04-06  |  7KB  |  259 lines

  1. /*
  2.  *      UniTerm demo program by Simon Poole 16.03.88
  3.  *
  4.  *      THIS IS REALLY JUST A QUICK HACK
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <osbind.h>
  10. #include "uni.h"
  11.  
  12. #define E_PINVALID 1
  13. #define E_PCORUP   2
  14. #define E_FOPEN    3
  15.  
  16. #define TMP    "TMPDIR"
  17. #define EDITOR "E:\\TEMPUS.PRG"
  18.  
  19. #define TRUE  1
  20. #define FALSE 0
  21.  
  22. char *getenv();
  23. UniStruct *atol();
  24. void exec_editor();
  25. char *get_tmp();
  26. void usage();
  27. void error();
  28. void dump_clip();
  29. void read_clip();
  30. void dump_hist();
  31.  
  32. main(argc, argv)
  33.  
  34. int  argc;
  35. char **argv;
  36.  
  37. {
  38.         char clip_name[127],hist_name[127],tmpname[255];
  39.         UniStruct  *UniParm;    
  40.         int history,clip,i;
  41.  
  42.         /* less than one argument is no good */
  43.         if (argc < 2) usage();
  44.  
  45.         /* init */
  46.         history=clip=FALSE;
  47.  
  48.         /*******************************************************/
  49.         /* Work thru commandline arguments                     */
  50.         /*******************************************************/        
  51.         for (i = 0;i < argc;i++) {
  52.            if (*argv[i] == '-') {
  53.               argv[i]++; /* strip off '-' */
  54.               switch(*argv[i]++) {
  55.                  case 'U' : if (*argv[i] == (char)0) {
  56.                                if (i < argc) {
  57.                                   i++;
  58.                                   UniParm = atol(argv[i]);
  59.                                }
  60.                                else usage();
  61.                             }
  62.                             else UniParm = atol(argv[i]);
  63.                             break;
  64.                  case 'H' : history = TRUE;
  65.                             break;
  66.                  case 'C' : clip = TRUE;
  67.                             break; 
  68.                  default  : usage();
  69.                             break;
  70.               }
  71.            }
  72.         }
  73.         if (!(history || clip)) usage();  /* we want to do something! */
  74.  
  75.         /* some probably useless checks */
  76.         if (UniParm == 0) error(E_PINVALID);
  77.         if (UniParm->PBLength < 50) error(E_PCORUP);
  78.  
  79.         /* assume everything is ok if we got this far */
  80.  
  81.         
  82.         clip_name[0] = '\000';
  83.         if (clip) {
  84.            /* get name of tmp file  */
  85.            strcpy(clip_name,get_tmp("C_XXXXXX"));
  86.            /* dump the clipboard    */
  87.            dump_clip(UniParm,clip_name);
  88.         }
  89.  
  90.         hist_name[0] = '\000';
  91.         if (history) {
  92.            /* get name of tmp file  */
  93.            strcpy(hist_name,get_tmp("H_XXXXXX"));
  94.            /* dump the clipboard    */
  95.            dump_hist(UniParm,hist_name);
  96.         }
  97.  
  98.         /* startup the editor    */
  99.         strcpy(tmpname,clip_name);
  100.         strcat(tmpname," ");
  101.         strcat(tmpname,hist_name); 
  102.         exec_editor(tmpname);
  103.  
  104.         /***************************************************/
  105.         /* remove the files and bye-bye                    */
  106.         /***************************************************/
  107.         if (history) unlink(hist_name);        
  108.         if (clip) {
  109.            /* read file back into clipboard */
  110.            read_clip(UniParm,clip_name);
  111.            unlink(clip_name);
  112.         }
  113.         exit(0);
  114. }
  115.  
  116. /**********************************************************/
  117. /* open the file for writing and dump the history to it   */
  118. /**********************************************************/
  119. void dump_hist(UniParm,tmpname)
  120.  
  121. char *tmpname;
  122. UniStruct  *UniParm;
  123.  
  124. {
  125.         FILE *fd;
  126.         register long i;
  127.         register char *Buf;     /* only needed for efficency */
  128.         long top,bottom;
  129.  
  130.         if ((fd = fopen(tmpname,"w")) == (FILE *)NULL) error(E_FOPEN);
  131.         Buf = UniParm->PBHistBuffer;
  132.         top = *UniParm->PBHistPtrAdr - (long)Buf;
  133.         bottom = *UniParm->PBHistBotAdr - (long)Buf;
  134.         if (top >= bottom) 
  135.            for (i=bottom;i < top;i++) putc(Buf[i],fd);
  136.         else {
  137.            for (i=bottom;i < UniParm->PBHistSize;i++) putc(Buf[i],fd);
  138.            for (i=0;i < top;i++) putc(Buf[i],fd);
  139.         }
  140.         fclose(fd);
  141. }
  142.  
  143. /**********************************************************/
  144. /* open the file for writing and dump the clipboard to it */
  145. /* map CR to \n                                           */
  146. /**********************************************************/
  147. void dump_clip(UniParm,tmpname)
  148.  
  149. char *tmpname;
  150. UniStruct  *UniParm;
  151.  
  152. {
  153.         FILE *fd;
  154.         register long i;
  155.         register char *ClipBuf,ch;
  156.         ClipStruct *ClipRec;           /* only needed for efficency */
  157.  
  158.         if ((fd = fopen(tmpname,"w")) == (FILE *)NULL) error(E_FOPEN);
  159.         ClipRec = UniParm->PBClipRecAdr;
  160.         ClipBuf = ClipRec->ClipBuffer;
  161.         for (i=0;i < ClipRec->ClipLen;i++) {
  162.            ch = ClipBuf[i];
  163.            if (ch=='\r') putc('\n',fd);
  164.            else putc(ch,fd);
  165.         }
  166.         fclose(fd);
  167. }
  168.  
  169. /****************************************************/
  170. /* copy the file back into the clipboard            */
  171. /* map \n to CR                                     */
  172. /****************************************************/
  173. void read_clip(UniParm,tmpname)
  174.  
  175. char *tmpname;
  176. UniStruct  *UniParm;
  177.  
  178. {
  179.         FILE *fd;
  180.         register long i;
  181.         register char *ClipBuf;
  182.         ClipStruct *ClipRec;           /* only needed for efficency */
  183.  
  184.         if ((fd = fopen(tmpname,"r")) == (FILE *)NULL) error(E_FOPEN);
  185.         ClipRec = UniParm->PBClipRecAdr;
  186.         ClipBuf = ClipRec->ClipBuffer;
  187.         if (fd != (FILE*)NULL) {
  188.            for (i=0;(!feof(fd))&&(i<UniParm->PBClipSize);i++){
  189.               ClipBuf[i]=getc(fd);
  190.               if (ClipBuf[i] == '\n') ClipBuf[i] = '\r';
  191.            }
  192.            ClipRec->ClipLen = --i;
  193.         }
  194.         else error(E_FOPEN); 
  195.         fclose(fd);
  196. }
  197.  
  198. /********************************************************/
  199. /* get a unique (hopefully) filename on tmp if possible */
  200. /********************************************************/
  201. char *get_tmp(fname)
  202.  
  203. char *fname;
  204.  
  205. {
  206.         char *tmpdir,tmpname[255];
  207.  
  208.         mktemp(fname); /* get a temp name */
  209.         if ((tmpdir = getenv(TMP)) != (char *)NULL) {
  210.            strcpy(tmpname,tmpdir);
  211.            strcat(tmpname,fname);
  212.            return(tmpname);
  213.         } 
  214.         else return(fname);
  215. }
  216.  
  217. /*****************************************************/
  218. /* startup the editor with the file(s) as argument(s)*/
  219. /*****************************************************/ 
  220. void exec_editor(fname)
  221.  
  222. char *fname;
  223.  
  224. {
  225.         char *editor,args[255];
  226.         int  result;
  227.  
  228.         editor = EDITOR;
  229.         /* args is a Pascal type string, copy fname in to it */
  230.         args[0] = (char)strlen(fname);
  231.         args[1] = '\000';
  232.         strcat(args,fname);
  233.         /* and exec the editor */
  234.         result = Pexec(0,editor,args,0L);
  235.         if (result) error(result);
  236. }
  237.  
  238. /**************************************/
  239. /* hey we've got a really stupid user */
  240. /**************************************/
  241. void usage()
  242.  
  243. {
  244.    puts("Usage: unidemo.prg -H -C -U address");
  245.    getchar(); 
  246.    exit(0);
  247. }
  248.  
  249. /**************************************/
  250. /* something went really wrong        */
  251. /**************************************/
  252. void error(num)
  253.  
  254. int num;
  255.  
  256. {
  257.    exit(num);
  258. }
  259.